package io.renren.common.query;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.entity.BaseEntity;
import io.renren.modules.com.entity.DictEntity;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * mybatis 实体映射查询
 * @author xwj
 * 2022/3/18 11:50
 */
public class QueryTool {
    public static void condition(QueryWrapper qw, BaseEntity form) {
         if(form != null) {
             try {
                 TableName tableName = (TableName)form.getClass().getDeclaredAnnotation(TableName.class);
                 Field[] fields = form.getClass().getDeclaredFields();

                 for(Field declaredField: fields) {
                     declaredField.setAccessible(true);
                     Object fieldValue = declaredField.get(form);
                     String fieldName = declaredField.getName();
                     String columnName = getFileColumnName(fieldName);
                     QueryCondition queryCondition = declaredField.getAnnotation(QueryCondition.class);
                     TableField tableField = declaredField.getAnnotation(TableField.class);

                     // 非字段 默认不查询
                     if(tableField != null && !tableField.exist()) {
                         continue;
                     }

                     // static field 不做查询条件
                     if(Modifier.isStatic(declaredField.getModifiers())) {
                         continue;
                     }

                     // 空值不做查询条件
                     if (fieldValue == null) {
                         continue;
                     }
                     // 空值
                     if(fieldValue instanceof String && StringUtils.isBlank(fieldValue.toString())) {
                         continue;
                     }

                     // 指定不做查询条件
                     if (queryCondition != null && !queryCondition.isCondition()) {
                         continue;
                     }

                     // 默认使用 eq
                     if(queryCondition == null) {
                         qw.eq(columnName, fieldValue);
                         continue;
                     }

                     Method method = queryCondition.value().getMethod();
                     System.out.println("===method"+ method);
                     method.invoke(qw, true, columnName, fieldValue);
                 }

                 if(!StringUtils.isNotBlank(form.getSidx())) {
                     if("DESC".equals(form.getOrder())) {
                         qw.orderByDesc(form.getSidx());
                     } else {
                         qw.orderByAsc(form.getSidx());
                     }
                 }
             } catch (Exception e) {
                 throw new RuntimeException("填充默认的SQL条件出错", e);
             }
         }
    }

    /**
     * 属性->字段;
     * 驼峰->下划线
     * @param name
     * @return
     */
    private static String getFileColumnName(String name) {
        return name.replaceAll("[A-Z]", "_$0").toLowerCase();
    }
}
